// pages/score/score.js

import API from "../../data/api";
import Util from "../../utils/util";

const App = getApp();
const Config = App.Config;

Page({
  data: {

    HOSTIMG: Config.HOSTIMG,

    select: false, // 是否点击查询
    selectScore: true, // 是否选择查询成绩
    Stnumber: null, // 学号
    Stpassword: null, // 密码
    listData: [], // 数据

    // 第一个选择栏
    selecExamScore: 0,
    arrayExamScore: ['成绩', '考试'],

    // 第二个选择栏  考试
    arrayExamTerm: [],
    arrayExamPostTerm: [],
    indexExamArray: 0,

    // 第二个选择栏  成绩
    arrayScoreTerm: [],
    arrayScorePostTerm: [],
    indexScoreArray: 0,

    // 设备信息
    windowWidth: App.globalData.systemInfo.windowWidth,
    windowHeight: App.globalData.systemInfo.windowHeight
  },

  onShow: function () {
    var that = this;

    if (App.globalData.openId == '' || App.globalData.openId == null) {
      Util.UI.loading(1000, '授权失败,请退出小程序重新登录');
      setTimeout(function () {
        wx.switchTab({
          url: '../index/index',
        })
      }, 1000)
    } else {
      App.Util.getStNumberPassword(App.globalData.openId) // 获取学号密码
        .then((res) => {
          // console.log(res.data.number);
          return App.Util.checkBind(res); // 检查用户是否绑定
        })
        .then((res) => {
          // console.log(res);
          that.setData({
            Stnumber: res.data.number,
            Stpassword: res.data.password,
          });
          return API.getScoreConfig(); // 获取成绩常量
        })
        .then((res) => {
          that.setData({
            arrayScoreTerm: res.data.data.scoreTerm,
            arrayScorePostTerm: res.data.data.scorePostTerm,
          });
          return API.getExamConfig(); // 获取考试常量
        })
        .then((res) => {
          that.setData({
            arrayExamTerm: res.data.data.examTerm,
            arrayExamPostTerm: res.data.data.examPostTerm,
          });
        })
    }
  },

  //选择查询什么
  pickerSubjectChange: function (e) {
    var that = this;
    var temp;
    if (e.detail.value == 0) {
      temp = true;
    } else {
      temp = false;
    }
    this.setData({
      selecExamScore: e.detail.value,
      selectScore: temp
    })
  },

  // 选择第几个学期
  pickerTermChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    var that = this;
    var temp;
    if (that.data.selectScore == true) {
      that.setData({
        indexScoreArray: e.detail.value
      })
    } else {
      that.setData({
        indexExamArray: e.detail.value
      })
    }
  },

  // 点击查询
  clickBtn: function () {
    var that = this;
    if (that.data.selectScore == true) {
      that.getScore();
    } else {
      that.getExam();
    }
  },

  // 查询考试
  getExam: function () {
    var that = this;
    API.getExam(that.data.arrayExamPostTerm[that.data.indexExamArray], that.data.Stnumber, that.data.Stpassword)
      .then((res) => {
        // console.log(res);
        if (res.data.error == Config.ERR.OK) {
          var newData = [];
          newData.push(...res.data.data);
          that.setData({
            listData: newData,
            select: !that.data.select
          });
        } else if (res.data.error == Config.ERR.ER) {
          Util.UI.loading(1000, res.data.data);
        }
      })
  },


  // 查询成绩
  getScore: function () {
    var that = this;
    API.getScore(that.data.arrayScorePostTerm[that.data.indexScoreArray], that.data.Stnumber, that.data.Stpassword)
      .then((res) => {
        // console.log(res);
        if (res.data.error == Config.ERR.OK) {
          var newData = [];
          newData.push(...res.data.data);
          that.setData({
            listData: newData,
            select: !that.data.select
          });
        } else if (res.data.error == Config.ERR.ER) {
          Util.UI.loading(1000, res.data.data);
        }
      });
  },

  // 点击背景
  clickBg: function () {
    var that = this;
    if (that.data.select == true) {
      that.setData({
        select: !that.data.select
      });
    }
  }

})